{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# about Directory and File\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 understand the module or package\n",
    "\n",
    "遇到新的module，先用三板斧：\n",
    "1. 导入\n",
    "2. 查三项\n",
    "3. 求助\n",
    "4. 如果是package，还要增加一项，检查目录，看有哪些module和subpackage（子包）\n",
    "\n",
    "```\n",
    "cd [包的目录]\n",
    "ls -l\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing aboutFile/introduction.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutFile/introduction.py\n",
    "import os                                    #导入\n",
    "print(type(os),os.__package__, os.__file__)  #查三项\n",
    "print(help(os))                              #求助\n",
    "                                             #由于是模块，则不需要继续了\n",
    "\n",
    "import os.path\n",
    "print(type(path),path.__package__, path.__file__)\n",
    "print(help(path))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/xingyongkang/github/aboutPython\n",
      "/Users/xingyongkang/d1\n"
     ]
    }
   ],
   "source": [
    "#%%writefile aboutFile/pathops.py\n",
    "import os\n",
    "from os import path\n",
    "\n",
    "curDir = os.getcwd()\n",
    "print(curDir)\n",
    "newDir = path.join(curDir,'../../d1')\n",
    "anewDir = path.abspath(newDir) \n",
    "print(anewDir)\n",
    "#print(help(path))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 files\n",
    "\n",
    "1. 文件的格式问题：\n",
    "  - 任何文件都是而二进制字节的序列。\n",
    "  - 文件的编码决定了怎样理解这些二进制。\n",
    "2. 文件内容的顺序：\n",
    "  - 按照字节read 或 write\n",
    "  - 按照编码 read 或write\n",
    "3. 文件的访问方式：\n",
    "  - read-only\n",
    "  - write-only\n",
    "  - read+write\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting aboutFile/binFile.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutFile/binFile.py\n",
    "#print(ord('b'),chr(97))\n",
    "def showbytes(b):\n",
    "    print(b)\n",
    "    print('----------------')\n",
    "    for i in b:\n",
    "        print('\\\\x{:x}'.format(i),end='')\n",
    "def writeFile():\n",
    "    bf = open('b1','bw')\n",
    "    block = b'\\x01\\x02\\x03\\x04\\x05'\n",
    "    bf.write(block)\n",
    "    bf.seek(1)\n",
    "    bf.write(b'\\x88')\n",
    "    bf.close()\n",
    "def readFile():\n",
    "    bf = open('b1','br')\n",
    "    #bf.seek(1)\n",
    "    block = bf.read(2)\n",
    "    #print(bf.tell())\n",
    "    showbytes(block)\n",
    "    bf.close()\n",
    "if __name__ == '__main__':\n",
    "    writeFile()\n",
    "    readFile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting aboutFile/textFile.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutFile/textFile.py\n",
    "def writeFile():\n",
    "    bf = open('b1','tw')\n",
    "    strings = 'abcdefghijklmnopqrstuvwxyz'\n",
    "    bf.write(strings)\n",
    "    #bf.seek()\n",
    "    #bf.write()\n",
    "    bf.close()\n",
    "def readFile():\n",
    "    bf = open('b1','tr')\n",
    "    #bf.seek(100)\n",
    "    strings = bf.read()    \n",
    "    #print(bf.tell())\n",
    "    print(strings)\n",
    "    bf.close()\n",
    "def readBinFile():\n",
    "    bf = open('b1','br')\n",
    "    blocks = bf.read()   \n",
    "    showbytes(blocks)\n",
    "    print(blocks)\n",
    "    bf.close()\n",
    "def showbytes(b):\n",
    "    print(b)\n",
    "    print('----------------')\n",
    "    for i in b:\n",
    "        print('\\\\x{:x}'.format(i),end='')\n",
    "    print('\\n')\n",
    "        \n",
    "if __name__ == '__main__':\n",
    "    writeFile()\n",
    "    readBinFile()\n",
    "    readFile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting aboutFile/hanFile.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutFile/hanFile.py\n",
    "def writeFile():\n",
    "    bf = open('b1','tw')\n",
    "    strings = 'abc我门def'\n",
    "    bf.write(strings)\n",
    "    #bf.seek()\n",
    "    #bf.write()\n",
    "    bf.close()\n",
    "def readFile():\n",
    "    bf = open('b1','tr')\n",
    "    #bf.seek()\n",
    "    strings = bf.read(4)    \n",
    "    print(bf.tell())\n",
    "    print(strings)\n",
    "    bf.close()\n",
    "def readBinFile():\n",
    "    bf = open('b1','br')\n",
    "    blocks = bf.read()    \n",
    "    showbytes(blocks)\n",
    "    bf.close()\n",
    "def showbytes(b):\n",
    "    print(b)\n",
    "    print('----------------')\n",
    "    for i in b:\n",
    "        print('\\\\x{:x}'.format(i),end='')\n",
    "    print('\\n')\n",
    "if __name__ == '__main__':\n",
    "    writeFile()\n",
    "    readBinFile()\n",
    "    readFile()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 stdin,stdout,stderr\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting aboutFile/stdIn.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutFile/stdIn.py\n",
    "import sys\n",
    "print(type(sys),sys.__package__)\n",
    "print(type(sys.stdin))\n",
    "\n",
    "temp = open('inputs','w')\n",
    "temp.write('12\\n34\\n')\n",
    "temp.close()\n",
    "\n",
    "sys.stdin = open('inputs','r')\n",
    "a = input('pls input a\\n')\n",
    "b = input('pls input b\\n')\n",
    "print(a,'+',b, '=', int(a)+int(b))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
